﻿
using DetectingKinectSencor.Tools;
using System.Data.SQLite;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DetectingKinectSencor._40_Tools;


namespace DetectingKinectSencor.BodyLanguage
{
    class MovementManager
    {
        private Skeleton skeleton;

        private Stopwatch counterTillStandingStarts;
        private Stopwatch counterHowLongStoodSameSpot;

        private bool hasMoved;
        private int movedID;

        private float hipCenterPositionXneu;
        private float hipCenterPositionZneu;
        private float hipCenterPositionXalt = -100000.0f;
        private float hipCenterPositionZalt;
        private Vector3 hipCenterPosition;
        private SQLiteConnection databaseConnection;
        private SQLiteCommand sqliteCommand;
        public DateTime startedCaptureTime;
        private DateTime startedMovingDetectedTimePoint;
        private int totalSecondsStoodSameSpot;



        public MovementManager(SQLiteConnection databaseConnection)
        {
            this.databaseConnection = databaseConnection;

            movedID = 0;

            // create a new SQL command:
            sqliteCommand = databaseConnection.CreateCommand();

            counterTillStandingStarts = new Stopwatch();
            counterHowLongStoodSameSpot = new Stopwatch();
        }

        internal void doWork(object skeleton)
        {
            this.skeleton = (Skeleton)skeleton;

            hipCenterPosition = Vector3.ToVector3(this.skeleton.Joints.Where(j => j.JointType ==
            JointType.HipCenter).First().Position);

            hipCenterPositionXneu = hipCenterPosition.X;
            hipCenterPositionZneu = hipCenterPosition.Z;

            // Find out if this is the first time doWork is executed, if so needs to initialize
            if (hipCenterPositionXalt == -100000.0f)
            {
                hipCenterPositionXalt = hipCenterPositionXneu;
                hipCenterPositionZalt = hipCenterPositionZneu;
            }

            // Speaker has moved
            if (Math.Abs(hipCenterPositionXalt - hipCenterPositionXneu) > XMLproperties.offsetMovementManager ||
                Math.Abs(hipCenterPositionZalt - hipCenterPositionZneu) > XMLproperties.offsetMovementManager)
            {
                if (counterTillStandingStarts.IsRunning)
                {
                    // Speaker has started to move
                    if (counterTillStandingStarts.Elapsed.Seconds > XMLproperties.secondsTillMovingDetected)
                    {
                        hipCenterPositionXalt = hipCenterPositionXneu;
                        hipCenterPositionZalt = hipCenterPositionZneu;

                        hasMoved = true;

                        if (counterHowLongStoodSameSpot.IsRunning)
                            counterHowLongStoodSameSpot.Stop();
                    }
                }
                // Start to detect if speaker moves
                else
                {
                    startedMovingDetectedTimePoint = DateTime.Now;
                    counterTillStandingStarts.Start();
                }
            }
            // Speaker has stopped to move
            else
            {
                if (hasMoved)
                {
                    try
                    {
                        movedID++;

                        totalSecondsStoodSameSpot = counterHowLongStoodSameSpot.Elapsed.Seconds + (int)Math.Ceiling(XMLproperties.secondsTillMovingDetected);

                        // Saved the detection into the sqlite DB, timespan is how long speaker stood on the same spot before started moving
                        sqliteCommand.CommandText =
                            "INSERT INTO movement (capturedTime, timepoint, timespanMinute, timespanSec) VALUES ('" + startedCaptureTime + "', '"
                                                                                                                    + startedMovingDetectedTimePoint + "', "
                                                                                                                    + counterHowLongStoodSameSpot.Elapsed.Minutes + ","
                                                                                                                    + totalSecondsStoodSameSpot
                                                                                                                    + ");";
                        sqliteCommand.ExecuteNonQuery();

                        // plus secondsTillMovingDetected second because this is the time, till the counter starts
                        Console.WriteLine("how long stood on the same spot: " + counterHowLongStoodSameSpot.Elapsed.Minutes + ":" +
                                                                                (counterHowLongStoodSameSpot.Elapsed.Seconds + XMLproperties.secondsTillMovingDetected));


                        Console.WriteLine("number of times moved " + movedID);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    // Reset counter 
                    counterHowLongStoodSameSpot.Reset();
                    hasMoved = false;
                }

                if (!counterHowLongStoodSameSpot.IsRunning)
                {
                    counterHowLongStoodSameSpot.Start();
                }

                if(counterTillStandingStarts.IsRunning)
                    counterTillStandingStarts.Reset();

            }
        }

    }

}
